package com.example.administrator.trains;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

public class register extends Activity implements OnClickListener  {

    private Button registerBtn;

    private EditText edt_passwd;
    private EditText edt_passwd_again;
    private EditText edt_id;
    private EditText edt_name;
    private EditText edt_citizen_id;
    private EditText edt_phone_num;

    private String passwd;
    private String passwd_again;
    private String id;
    private String name;
    private String citizen_id;
    private String phone_num;

    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if(msg.what==2){
                Toast.makeText(register.this,"信息重复，注册失败",Toast.LENGTH_LONG).show();
            }
            if(msg.what==3){
                Toast.makeText(register.this,"注册成功,亲,可以登录喽~~",Toast.LENGTH_LONG).show();
            }
            if(msg.what==4){
                Toast.makeText(register.this,"服务器没有响应，请重新注册",Toast.LENGTH_LONG).show();
            }
            if(msg.what==5){
                Toast.makeText(register.this,"服务器断开，尝试重连",Toast.LENGTH_LONG).show();
                Socket.connector.releaseSocket();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(getWindow().FEATURE_NO_TITLE);
        setContentView(R.layout.activity_register);
        registerBtn=(Button) findViewById(R.id.btn_register);
        registerBtn.setOnClickListener(this);
        registerBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape2));
                        break;
                    case MotionEvent.ACTION_UP:
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape));
                        registerBtn.performClick();
                        break;
                }

                return true;
            }
        });

        edt_passwd = (EditText)findViewById(R.id.edt_input_passwd);
        edt_passwd_again = (EditText)findViewById(R.id.edt_input_passwd_again);
        edt_id = (EditText)findViewById(R.id.edt_get_id);
        edt_name = (EditText)findViewById(R.id.edt_get_name);
        edt_citizen_id = (EditText)findViewById(R.id.edt_get_citizen_id);
        edt_phone_num = (EditText)findViewById(R.id.edt_get_phone_num);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent i = new Intent(register.this, MainActivity.class);
            startActivity(i);
            this.finish();
        }
        return true;
    }
    @Override
    public void onClick(View v) {
        if (edt_passwd.getText().toString().matches("[0-9a-zA-Z]{5,10}") &&
                edt_passwd_again.getText().toString().matches("[0-9a-zA-Z]{5,10}")) {
            passwd = edt_passwd.getText().toString();
            passwd_again = edt_passwd_again.getText().toString();
        } else {
            Toast.makeText(register.this, "密码格式不对,请不要输入字母以外的字符,要求为5到10个字符组成", Toast.LENGTH_LONG).show();
            return;
        }

        if (passwd.compareTo(passwd_again) != 0) {
            Toast.makeText(register.this, "两次密码不匹配", Toast.LENGTH_LONG).show();
            return;
        }

        if (edt_id.getText().toString().matches("[0-9a-zA-Z]{5,10}")) {
            id = edt_id.getText().toString();
        } else {
            Toast.makeText(register.this, "账号格式不对，请不要输入字母以外的字符,要求为5到10个字符组成", Toast.LENGTH_LONG).show();
            return;
        }

        if (edt_name.getText().toString().matches("[\\u4E00-\\u9FA5]{2,5}")) {
            name = edt_name.getText().toString();
        } else {
            Toast.makeText(register.this, "姓名格式不对，请输入中文,要求为二到五个中文", Toast.LENGTH_LONG).show();
            return;
        }

        if (edt_citizen_id.getText().toString().matches("[0-9]{18}")) {
            citizen_id = edt_citizen_id.getText().toString();
        } else {
            Toast.makeText(register.this, "身份证格式不对，请不要输入数字以外的字符,同时必须是18位数", Toast.LENGTH_LONG).show();
            return;
        }

        if (edt_phone_num.getText().toString().matches("[0-9]{11}")) {
            phone_num = edt_phone_num.getText().toString();
        } else {
            Toast.makeText(register.this, "电话号码格式不对，请不要输入数字以外的字符,同时必须是11位数", Toast.LENGTH_LONG).show();
            return;
        }

        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.btn_register://注册
                if (Socket.mClient != null) {
                    try {
                        /*发送数据*/
                        Socket.connector.send(Socket.connector.package_data(Socket.connector.LOGIN_CMD_TYPE,
                                Socket.connector.REGISTER_CMD, "00", id + CommunicationData.empty +
                                        passwd + CommunicationData.empty +
                                        name + CommunicationData.empty +
                                        citizen_id + CommunicationData.empty +
                                        phone_num + CommunicationData.empty));
                        /*接收服务端是否成功接收信息的ack*/
                        Socket.connector.recv_ack();
                        if (CommunicationData.ack == "01") {
                            /*接收服务端是否成功注册的ack*/
                            Socket.connector.recv_ack();
                            if (CommunicationData.ack == Socket.connector.REGISTER_SUCCESS_ACK) {
                                Intent i = new Intent(register.this, MainActivity.class);
                                startActivity(i);

                                Message m = new Message();
                                m.what = 3;
                                myHandler.sendMessage(m);
                            }
                            /*注册失败*/
                            if (CommunicationData.ack == Socket.connector.REGISTER_USER_REPEAT_ERR_ACK) {
                                Intent i = new Intent(register.this, MainActivity.class);
                                startActivity(i);

                                Message m = new Message();
                                m.what = 2;
                                myHandler.sendMessage(m);
                            }

                            this.finish();
                        } else {
                            Message m = new Message();
                            m.what = 4;
                            myHandler.sendMessage(m);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    Message m = new Message();
                    m.what = 5;
                    myHandler.sendMessage(m);
                }
                break;
            default:
                break;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
